/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Controladores.ControladorHoteleria;
import Controladores.ControladorUsuario;
import Logico.Hoteleria;
import Logico.Usuario;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 *
 * @author vladimir
 */
public class HoteleriaGUI extends javax.swing.JPanel {

    /**
     * Creates new form UsuarioGUI
     */
    ControladorHoteleria controladorHoteleria;
    String nombre;
    String direccion;
    String telefono;
    String email;
    String paginaweb;
    String infohabitacionescosto;
    List<Hoteleria> listaHoteles;
    int indice;

    public HoteleriaGUI() {
        initComponents();
        controladorHoteleria = new ControladorHoteleria();
        listaHoteles = controladorHoteleria.listarHotelerias();
        indice = 0;
        inicializarTabla();
    }

    void inicializarTabla() {
        tablaHoteleria.removeAll();
        DefaultTableModel modelo = (DefaultTableModel) tablaHoteleria.getModel();
        modelo.setRowCount(0);

        for (int i = 0; i < listaHoteles.size(); i++) {
            String temp[] = new String[6];

            temp[0] = listaHoteles.get(i).getNombre();
            temp[1] = listaHoteles.get(i).getDireccion();
            temp[2] = listaHoteles.get(i).getTelefono();
            temp[3] = listaHoteles.get(i).getEmail();
            temp[4] = listaHoteles.get(i).getPaginaweb();
            temp[5] = listaHoteles.get(i).getInfohabitacionescosto();

            Object nuevo[] = null;
            modelo.addRow(nuevo);
            tablaHoteleria.setModel(modelo);
            for (int j = 0; j < temp.length; j++) {
                tablaHoteleria.setValueAt(temp[j], i, j);
            }
        }

        if (listaHoteles.size() > 0) {
            indice = 0;
            actualizarFormulario();
        }

    }

    void actualizarFormulario() {
        nombre = listaHoteles.get(indice).getNombre();
        campoNombre.setText(nombre);
        direccion = listaHoteles.get(indice).getDireccion();
        campoDireccion.setText(direccion);
        telefono = listaHoteles.get(indice).getTelefono();
        campoTelefono.setText(telefono);
        email = listaHoteles.get(indice).getEmail();
        campoEmail.setText(email);
        paginaweb = listaHoteles.get(indice).getPaginaweb();
        campoWeb.setText(paginaweb);
        infohabitacionescosto = listaHoteles.get(indice).getInfohabitacionescosto();
        campoCosto.setText(infohabitacionescosto);
    }

    void limpiarFormulario() {
        nombre = "";
        campoNombre.setText(nombre);
        direccion = "";
        campoDireccion.setText(direccion);
        telefono = "";
        campoTelefono.setText(telefono);
        email = "";
        campoEmail.setText(email);
        paginaweb = "";
        campoWeb.setText(paginaweb);
        infohabitacionescosto = "";
        campoCosto.setText(infohabitacionescosto);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        botonGuardar = new javax.swing.JButton();
        botonActualizar = new javax.swing.JButton();
        botonEliminar = new javax.swing.JButton();
        botonBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaHoteleria = new javax.swing.JTable();
        botonAnterior = new javax.swing.JButton();
        botonSiguiente = new javax.swing.JButton();
        botonLimpiar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        campoDireccion = new javax.swing.JTextField();
        campoTelefono = new javax.swing.JTextField();
        campoEmail = new javax.swing.JTextField();
        campoWeb = new javax.swing.JTextField();
        campoCosto = new javax.swing.JTextField();

        jLabel2.setText("Nombre (*)");

        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonActualizar.setText("Actualizar");
        botonActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonActualizarActionPerformed(evt);
            }
        });

        botonEliminar.setText("Eliminar");
        botonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarActionPerformed(evt);
            }
        });

        botonBuscar.setText("Buscar");
        botonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBuscarActionPerformed(evt);
            }
        });

        tablaHoteleria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Direccion", "Telefono", "Email", "Web", "Costo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaHoteleria);

        botonAnterior.setText("<<");
        botonAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAnteriorActionPerformed(evt);
            }
        });

        botonSiguiente.setText(">>");
        botonSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSiguienteActionPerformed(evt);
            }
        });

        botonLimpiar.setText("Limpiar");
        botonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLimpiarActionPerformed(evt);
            }
        });

        jLabel6.setText("Dirección (*)");

        jLabel7.setText("Telefono");

        jLabel8.setText("E-Mail");

        jLabel9.setText("Web");

        jLabel10.setText("Costo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(botonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9))
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(campoWeb, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(botonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                                .addComponent(botonAnterior)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(botonSiguiente))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel10))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(campoCosto, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(campoDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(campoWeb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(campoEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(campoCosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(97, 97, 97)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonGuardar)
                    .addComponent(botonActualizar)
                    .addComponent(botonEliminar)
                    .addComponent(botonBuscar)
                    .addComponent(botonAnterior)
                    .addComponent(botonSiguiente)
                    .addComponent(botonLimpiar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        // TODO add your handling code here:
        if (!campoNombre.getText().equals("")
                && !campoDireccion.getText().equals("")
                && !campoTelefono.getText().equals("")
                && !campoEmail.getText().equals("")
                && !campoWeb.getText().equals("")
                && !campoCosto.getText().equals("")) {

            nombre = campoNombre.getText();
            direccion = campoDireccion.getText();
            telefono = campoTelefono.getText();
            email = campoEmail.getText();
            paginaweb = campoWeb.getText();
            infohabitacionescosto = campoCosto.getText();

            boolean registrado = controladorHoteleria.guardar(nombre, direccion, telefono, email, paginaweb, infohabitacionescosto);

            if (registrado) {
                JOptionPane.showMessageDialog(null, "Hotel registrado con exito");
                listaHoteles = controladorHoteleria.listarHotelerias();
                inicializarTabla();
            } else {
                JOptionPane.showMessageDialog(null, "Hotel no se pudo registrar");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingrese los datos del hotel");
        }

    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonActualizarActionPerformed
        // TODO add your handling code here:
        
        if (campoNombre.getText().equals("") || campoDireccion.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Los campos obligatorios (*) no deben estar vacios");

        } else {
            nombre = campoNombre.getText();
            direccion = campoDireccion.getText();
            telefono = campoTelefono.getText();
            email = campoEmail.getText();
            paginaweb = campoWeb.getText();
            infohabitacionescosto = campoCosto.getText();

            boolean actualizado = controladorHoteleria.modificar(nombre, direccion, telefono, email, paginaweb, infohabitacionescosto);

            if (actualizado) {
                JOptionPane.showMessageDialog(null, "Hotel actualizado con exito");
            } else {
                JOptionPane.showMessageDialog(null, "Hotel no pudo ser actualizado");
            }
        }
    }//GEN-LAST:event_botonActualizarActionPerformed

    private void botonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarActionPerformed
        // TODO add your handling code here:
        if (!campoDireccion.getText().equals("")) {
            direccion = campoDireccion.getText();
            boolean borrado = controladorHoteleria.borrar(direccion);

            if (borrado) {
                JOptionPane.showMessageDialog(null, "Hotel eliminado con exito");
                limpiarFormulario();
                listaHoteles = controladorHoteleria.listarHotelerias();
                inicializarTabla();
            } else {
                JOptionPane.showMessageDialog(null, "Hotel no se pudo eliminar");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingrese una direccion");
        }
    }//GEN-LAST:event_botonEliminarActionPerformed

    private void botonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBuscarActionPerformed
        // TODO add your handling code here:
        if (!campoDireccion.getText().equals("")) {
            Hoteleria h = controladorHoteleria.buscar(direccion);
            if (h != null) {
                listaHoteles.removeAll(listaHoteles);
                listaHoteles.add(h);
                inicializarTabla();
            } else {
                JOptionPane.showMessageDialog(null, "No se encontro el hotel con direccion " + direccion);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingrese una direccion");
        }
    }//GEN-LAST:event_botonBuscarActionPerformed

    private void botonAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAnteriorActionPerformed
        // TODO add your handling code here:
        if (indice > 0) {
            indice--;
            actualizarFormulario();
        }
    }//GEN-LAST:event_botonAnteriorActionPerformed

    private void botonSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSiguienteActionPerformed
        // TODO add your handling code here:
        if (indice < listaHoteles.size() - 1) {
            indice++;
            actualizarFormulario();
        }
    }//GEN-LAST:event_botonSiguienteActionPerformed

    private void botonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLimpiarActionPerformed
        // TODO add your handling code here:
        limpiarFormulario();
    }//GEN-LAST:event_botonLimpiarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonActualizar;
    private javax.swing.JButton botonAnterior;
    private javax.swing.JButton botonBuscar;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton botonLimpiar;
    private javax.swing.JButton botonSiguiente;
    private javax.swing.JTextField campoCosto;
    private javax.swing.JTextField campoDireccion;
    private javax.swing.JTextField campoEmail;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoTelefono;
    private javax.swing.JTextField campoWeb;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaHoteleria;
    // End of variables declaration//GEN-END:variables
}
